package jia.game;

import java.net.MalformedURLException;
import jia.core.Compass;
import jia.core.EntryPoint;
import jia.core.Module;
import jia.core.Space;
import jia.core.Wall;
import jia.core.event.AttackEvent;
import jia.core.event.DamageEvent;
import jia.core.event.TextEvent;
import jia.core.flags.WallFlags;
import jia.core.flags.DamageEventFlags;
import jia.core.flags.EventFlags;
import jia.core.event.eventdata.AttackEventData;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;
import org.apache.commons.lang.StringUtils;

/**
 * Test Game class.
 */
public class TestGame {
    private static final Logger log = Logger.getLogger(TestGame.class);
    private Module testModule;
    
    public TestGame() throws MalformedURLException {
        
        testModule = new Module();
        
        String imageParserClass = "jia.core.graphics.FirstPersonWallImageParser";
        ModuleConstructor constructor = new ModuleConstructor();
        constructor.buildModule(testModule);
        
        //load the xml file from an external resouce
        
        //iterate through the file and build the module
        try {
            Document doc;
            SAXReader xmlReader = new SAXReader();
            File aFile = new File("resources/levels/level1.xml");
            doc = xmlReader.read(aFile);
            
            Element root = doc.getRootElement();
            testModule.setName(root.attribute("name").getValue());
            
            //set the starting location and direction for this level
            EntryPoint ep = new EntryPoint();
            if (root.attribute("startDirection").getValue().equals("S")){
                ep.setEntryFacing(Compass.SOUTH);
            }else if (root.attribute("startDirection").getValue().equals("N")){
                ep.setEntryFacing(Compass.NORTH);
            }else if (root.attribute("startDirection").getValue().equals("E")){
                ep.setEntryFacing(Compass.EAST);
            }else if (root.attribute("startDirection").getValue().equals("W")){
                ep.setEntryFacing(Compass.WEST);
            }
            
            ep.setEntryPosition(Integer.parseInt(root.attribute("startx").getValue()), Integer.parseInt(root.attribute("starty").getValue()));
            testModule.addEntryPoint(ep);
            
            Iterator elementIterator = root.elementIterator();
            
            //loop through the spaces
            while(elementIterator.hasNext()){
                Element elment = (Element)elementIterator.next();
                
                //create a new space
                Space newSpace = new Space();
				//System.out.println(elment.getData().toString());
                
                //set the backdrop image for this space
                newSpace.setBackdropImageName("images/backdrops/" + elment.element("backdrop").getStringValue());
                
                //TODO:  add logic to add an assigned zone to this space
                
                //TODO:  add logic to assign an event to this space.
                
                //loop through the walls
                Iterator childIterator = elment.elementIterator("walls");
                while(childIterator.hasNext()){
                    Element child = (Element)childIterator.next();
                    Iterator wallIter = child.elementIterator();
                    while(wallIter.hasNext()){
                        Element wallElement = (Element)wallIter.next();
                        if (GenericFlags.debugMessages){
                            log.debug(wallElement.getName());
                        }
                        Wall newWall = new Wall();


						//had a bizarre error where the filename was returning with spaces
						String fixedName = StringUtils.deleteWhitespace(wallElement.getStringValue());
						//System.out.println(fixedName);
						newWall.setWallImageName("images/walls/" + fixedName);
                        newWall.setWallImageParserName(imageParserClass);
						
                        if (wallElement.attribute("position").getValue().equals("N")){
                            newWall.setFacing(Compass.NORTH);
                        }else if(wallElement.attribute("position").getValue().equals("S")){
                            newWall.setFacing(Compass.SOUTH);
                        }else if(wallElement.attribute("position").getValue().equals("E")){
                            newWall.setFacing(Compass.EAST);
                        }else if(wallElement.attribute("position").getValue().equals("W")){
                            newWall.setFacing(Compass.WEST);
                        }
                        
                        WallFlags wallFlags = new WallFlags();
                        if (wallElement.attribute("blocked").getValue().equals("Y")){
                            wallFlags.setFlag(WallFlags.BLOCKED);
                        }
                        if (wallElement.attribute("false").getValue().equals("Y")){
                            wallFlags.setFlag(WallFlags.FALSE_DOOR);
                        }
                        if (wallElement.attribute("locked").getValue().equals("Y")){
                            wallFlags.setFlag(WallFlags.LOCKED);
                        }
                        if (wallElement.attribute("locked_wizard").getValue().equals("Y")){
                            wallFlags.setFlag(WallFlags.LOCKED_WIZARD);
                        }
                        if (wallElement.attribute("open").getValue().equals("Y")){
                            wallFlags.setFlag(WallFlags.OPEN);
                        }
                        if (wallElement.attribute("secret").getValue().equals("Y")){
                            wallFlags.setFlag(WallFlags.SECRET);
                        }
                        newWall.setFlags(wallFlags);
                        newSpace.addWall(newWall);
                    }
                }
                
                //add this newly created space to the loaded level
                testModule.setSpace(Integer.parseInt(elment.attribute("xpos").getValue()), Integer.parseInt(elment.attribute("ypos").getValue()), newSpace);
                
            }
        } catch(DocumentException e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        // set a new name
        // testModule.setName("Test Dungeon Module");
        
        // add some zones to the module
        //testModule.addZone(new Zone("Hallway"));
        //testModule.addZone(new Zone("Room"));
        
        //String back1 = "images/stoneFloor01.png";
        //String back2 = "images/stoneFloor02.png";
        
        // we should already have an array of spaces 16 x 16, so modify them
        
        
        //String stoneWall = "images/stoneWall01.png";
        // String stoneDoor = "images/stoneDoor01.png";
        
        // WallImage stoneWall = wallParser
        // .getWallsetImage("images/stoneWall01.png");
        // WallImage stoneDoor = wallParser
        // .getWallsetImage("images/stoneDoor01.png");
        
        // Row 0
        
        // Row 1
        
        
//
//		testModule.setSpace(1, 1, new Space());
//        testModule.getSpace(1, 1).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//        testModule.getSpace(1, 1).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//        testModule.getSpace(1, 1).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//
//        testModule.setSpace(3, 1, new Space());
//        testModule.getSpace(3, 1).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//        testModule.getSpace(3, 1).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//        testModule.setSpace(4, 1, new Space());
//        testModule.getSpace(4, 1).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//
//        testModule.setSpace(5, 1, new Space());
//        testModule.getSpace(5, 1).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//        // testModule.getSpace(5, 1).addWall( new Wall(stoneWall,
//        // imageParserClass));
//
//        testModule.setSpace(6, 1, new Space());
//        testModule.getSpace(6, 1).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//        testModule.getSpace(6, 1).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//        testModule.getSpace(6, 1).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//
//        // Row 2
//        testModule.setSpace(1, 2, new Space());
//        testModule.getSpace(1, 2).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//        testModule.getSpace(1, 2).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//        testModule.setSpace(3, 2, new Space());
//        testModule.getSpace(3, 2).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//        testModule.getSpace(3, 2).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//        testModule.setSpace(4, 2, new Space());
//        testModule.getSpace(4, 2).addWall(new Wall(stoneDoor, imageParserClass, Compass.SOUTH));
//        testModule.getSpace(4, 2).getWall(Compass.SOUTH).getFlags().setFlag(WallFlags.OPEN);
//
//
//		testModule.setSpace(5, 2, new Space());
//        testModule.getSpace(5, 2).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//        testModule.getSpace(5, 2).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//
//        // Row 3
//        testModule.setSpace(1, 3, new Space());
//        testModule.getSpace(1, 3).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//        testModule.getSpace(1, 3).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//        testModule.setSpace(4, 3, new Space());
//        testModule.getSpace(4, 3).addWall(new Wall(stoneDoor, imageParserClass, Compass.NORTH));
//        testModule.getSpace(4, 3).getWall(Compass.NORTH).getFlags().setFlag(WallFlags.OPEN);
//        testModule.getSpace(4, 3).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//        testModule.getSpace(4, 3).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//        // Row 4
//        testModule.setSpace(1, 4, new Space());
//        testModule.getSpace(1, 4).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//        testModule.getSpace(1, 4).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//        testModule.setSpace(2, 4, new Space());
//        testModule.getSpace(2, 4).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//        testModule.getSpace(2, 4).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//        testModule.setSpace(3, 4, new Space());
//        testModule.getSpace(3, 4).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//        testModule.getSpace(3, 4).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//        testModule.setSpace(4, 4, new Space());
//
//        testModule.getSpace(4, 4).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//
//        testModule.setSpace(4, 5, new Space());
//        testModule.getSpace(4, 5).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//        testModule.getSpace(4, 5).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(4, 6, new Space());
//		testModule.getSpace(4, 6).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//		testModule.getSpace(4, 6).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(4, 7, new Space());
//		testModule.getSpace(4, 7).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//		testModule.getSpace(4, 7).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//
//
//		testModule.setSpace(4, 8, new Space());
//		testModule.getSpace(4, 8).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//		testModule.getSpace(4, 8).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//		testModule.setSpace(5, 8, new Space());
//		testModule.getSpace(5, 8).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//		testModule.getSpace(5, 8).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//		testModule.setSpace(6, 8, new Space());
//		testModule.getSpace(6, 8).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//		testModule.getSpace(6, 8).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//		testModule.setSpace(7, 8, new Space());
//		testModule.getSpace(7, 8).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//		testModule.getSpace(7, 8).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//		testModule.setSpace(8, 7, new Space());
//		testModule.getSpace(8, 7).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//		testModule.getSpace(8, 7).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(9, 7, new Space());
//		testModule.getSpace(9, 7).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//
//		testModule.setSpace(10, 7, new Space());
//		testModule.getSpace(10, 7).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//		testModule.getSpace(10, 7).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//
//		testModule.setSpace(8,8, new Space());
//		testModule.setSpace(9,8, new Space());
//
//		testModule.setSpace(10, 8, new Space());
//		testModule.getSpace(10, 8).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//
//
//		testModule.setSpace(8, 9, new Space());
//		testModule.getSpace(8, 9).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//		testModule.getSpace(8, 9).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//		testModule.setSpace(9, 9, new Space());
//		testModule.getSpace(9, 9).addWall(new Wall(stoneDoor, imageParserClass, Compass.SOUTH));
//		testModule.getSpace(9, 9).getWall(Compass.SOUTH).getFlags().setFlag(WallFlags.OPEN);
//
//		testModule.setSpace(10, 9, new Space());
//		testModule.getSpace(10, 9).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//		testModule.getSpace(10, 9).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//
//		testModule.setSpace(9, 10, new Space());
//		testModule.getSpace(9, 10).addWall(new Wall(stoneDoor, imageParserClass, Compass.NORTH));
//		testModule.getSpace(9, 10).getWall(Compass.NORTH).getFlags().setFlag(WallFlags.OPEN);
//		testModule.getSpace(9, 10).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//		testModule.getSpace(9, 10).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(4, 11, new Space());
//		testModule.getSpace(4, 11).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//		testModule.getSpace(4, 11).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(5, 11, new Space());
//		testModule.getSpace(5, 11).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//
//		testModule.setSpace(6, 11, new Space());
//		testModule.getSpace(6, 11).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//		testModule.getSpace(6, 11).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//
//		testModule.setSpace(9, 11, new Space());
//		testModule.getSpace(9, 11).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//		testModule.getSpace(9, 11).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(4, 12, new Space());
//		testModule.getSpace(4, 12).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(5,12, new Space());
//		testModule.setSpace(6, 12, new Space());
//		testModule.getSpace(6, 12).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//
//
//		testModule.setSpace(9, 12, new Space());
//		testModule.getSpace(9, 12).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//		testModule.getSpace(9, 12).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(4, 13, new Space());
//		testModule.getSpace(4, 13).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//		testModule.getSpace(4, 13).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//		testModule.setSpace(5, 13, new Space());
//		testModule.getSpace(5, 13).addWall(new Wall(stoneDoor, imageParserClass, Compass.SOUTH));
//		testModule.getSpace(5, 13).getWall(Compass.SOUTH).getFlags().setFlag(WallFlags.OPEN);
//
//		testModule.setSpace(6, 13, new Space());
//		testModule.getSpace(6, 13).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//		testModule.getSpace(6, 13).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//
//		testModule.setSpace(9, 13, new Space());
//		testModule.getSpace(9, 13).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//		testModule.getSpace(9, 13).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(5, 14, new Space());
//		testModule.getSpace(5, 14).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//		testModule.getSpace(5, 14).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(9, 14, new Space());
//		testModule.getSpace(9, 14).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//		testModule.getSpace(9, 14).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//
//		testModule.setSpace(5, 15, new Space());
//		testModule.getSpace(5, 15).addWall(new Wall(stoneWall, imageParserClass, Compass.WEST));
//		testModule.getSpace(5, 15).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//		testModule.setSpace(6, 15, new Space());
//		testModule.getSpace(6, 15).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//		testModule.getSpace(6, 15).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//		testModule.setSpace(7, 15, new Space());
//		testModule.getSpace(7, 15).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//		testModule.getSpace(7, 15).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//		testModule.setSpace(8, 15, new Space());
//		testModule.getSpace(8, 15).addWall(new Wall(stoneWall, imageParserClass, Compass.NORTH));
//		testModule.getSpace(8, 15).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//
//		testModule.setSpace(9, 15, new Space());
//		testModule.getSpace(9, 15).addWall(new Wall(stoneWall, imageParserClass, Compass.SOUTH));
//		testModule.getSpace(9, 15).addWall(new Wall(stoneWall, imageParserClass, Compass.EAST));
//
        
        
        
        
        
        
        // NOW THAT THE WALLS HAVE BEEN PLACED, PUT AN EVENT IN ONE OF THE SQUARES
        TextEvent txtEvt = new TextEvent();
        txtEvt.setEncounterText("Welcome to the dungeon!");
        txtEvt.getFlags().setFlag(EventFlags.ONLY_ONCE);
        testModule.getSpace(1, 2).setEvent(txtEvt);
        
        DamageEvent dmgEvt = new DamageEvent();
        dmgEvt.setEncounterText("Pit spikes fall on you!");
        dmgEvt.getFlags().setFlag(EventFlags.ONLY_ONCE);
        
        dmgEvt.setDamageDice(4);
        dmgEvt.setNumberDice(1);
        dmgEvt.setDamageFlags(new DamageEventFlags());
        dmgEvt.getDamageFlags().setFlag(DamageEventFlags.DAMAGE_RESULT_FROM_TRAP);
        dmgEvt.getDamageFlags().setFlag(DamageEventFlags.AFFECTS_ENTIRE_PARTY);
        testModule.getSpace(1, 3).setEvent(dmgEvt);
        
        AttackEvent attackEvt = new AttackEvent();
        AttackEventData evtData = new AttackEventData();
        attackEvt.getFlags().setFlag(EventFlags.ONLY_ONCE);
        evtData.setEncounterText("You have been ambushed!");
        attackEvt.setAttackData(evtData);
        testModule.getSpace(4, 2).setEvent(attackEvt);
        
        /**
         *
         * // SET THE BACKGROUND IMAGES FOR EEACH SPACE
         * int imageToggler = 1;
         *
         * for (int i = 0; i < 16; i++) {
         * for (int j = 0; j < 16; j++) {
         * Space thisSpace = testModule.getSpace(i, j);
         * if (thisSpace == null) continue;
         *
         * if (imageToggler == 1) {
         * thisSpace.setBackdropImageName(back1);
         * imageToggler = 2;
         * } else if (imageToggler == 2) {
         * thisSpace.setBackdropImageName(back2);
         * imageToggler = 1;
         * } else {
         * // default it if a problem
         * thisSpace.setBackdropImageName(back1);
         * }
         * // TODO: set specific flags for this space?
         * }
         * }
         **/
        
    }
    
    public Module getTestModule() {
        return testModule;
    }
}
